package com.gm.lib.base;

import android.os.Bundle;
import android.view.MotionEvent;

import com.gm.ui.base.BaseActivity;

/**
 * Created by cx on 15/4/21.
 */
public class GMBaseActivity extends BaseActivity {

    private boolean isSlideOut = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 是否支持滑动退出
     * @param isSlideOut
     */
    public void setSlideOut(boolean isSlideOut) {
        this.isSlideOut = isSlideOut;
    }

    float x1, x2, y1, y2;
    /**
     * 距离
     */
    final int SLIDDING_LENGTH = 100;
    /**
     * 时间
     */
    final int TOUCH_TIME = 200;
    long firstTouchTime, lastTouchTime;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (!isSlideOut) {
            return super.dispatchTouchEvent(event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = event.getX();
                y1 = event.getY();
                firstTouchTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                lastTouchTime = System.currentTimeMillis();
                if (lastTouchTime - firstTouchTime > TOUCH_TIME) {
                    break;
                }
                x2 = event.getX();
                y2 = event.getY();
                float differenceInX = x2 - x1;
                float differenceInY = y2 - y1;
                // Use dx and dy to determine the direction
                if (Math.abs(differenceInX) > Math.abs(differenceInY)) {
                    if (Math.abs(differenceInX) >= SLIDDING_LENGTH) {
                        if (differenceInX > 0) {
                            finish();
                            return true;
                        }
                    }
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }

}
